/**
 * Handle resources, ideally all resources access should be done from here
 */
package uk.ac.lkl.common.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.URL;

import javax.swing.ImageIcon;
/**
 * @author yish
 *
 */
public class Resources {

	private static ClassLoader cl = Resources.class.getClassLoader();
	
	/**
	 * get the text of a resource file.
	 * 
	 * @param id the resource identifier
	 * @return
	 */
	public static String getResourceString(String id) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(cl.getResourceAsStream(id)));
		StringBuilder sb = new StringBuilder();
		
		String tmp = in.readLine();
		while (tmp != null) {
			sb.append(tmp);
			tmp = in.readLine();
		}
		return sb.toString();
	}
	
	
	public static InputStream getResourceInputStream(String id)throws IOException{
		InputStream is = cl.getResourceAsStream(id);
		return is;
	}
	
	
	public static ImageIcon getIcon(String id){
		if (id == null) return null;
		URL imageURL = cl.getResource(id);
		if (imageURL == null) return null;
        return new ImageIcon(imageURL);
	}
}
